namespace Deppton.Web.Controllers
{
    using System.Linq;
    using System.Security.Principal;
    using System.Web.Mvc;
    using System.Xml;
    using System.Xml.Linq;
    using Deppton.Model;
    using Deppton.Model.Xml;
    using Deppton.Repositories;
    using Deppton.Repositories.Filesystem;

    /// <summary>
    /// Synchronization controller.
    /// </summary>
    public class SynchronizationController : Controller
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SynchronizationController"/> class.
        /// </summary>
        public SynchronizationController()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SynchronizationController"/> class.
        /// </summary>
        /// <param name="surveysRepository">The surveys repository.</param>
        public SynchronizationController(ISurveysRepository surveysRepository)
        {
            this.SurveysRepository = surveysRepository;
        }

        /// <summary>
        /// Gets or sets the surveys repository.
        /// </summary>
        /// <value>The surveys repository.</value>
        private ISurveysRepository SurveysRepository
        {
            get;
            set;
        }

        /// <summary>
        /// Retrieves the pending surveys for the current logged in user.
        /// </summary>
        /// <param name="userIdentity">The user identity.</param>
        /// <returns>The pending surveys.</returns>
        public ActionResult RetrievePendingSurveys(IPrincipal userPrincipal)
        {
            var surveys = this.SurveysRepository.FindPendingSurveysByUser(userPrincipal.Identity.Name);

            var document = new XDocument(
                new XElement(
                    "surveys",
                    surveys.Select(s => SurveyXmlHelper.ConvertToXDocument(s).Root).ToArray()));

            return this.Content(document.ToString());
        }

        /// <summary>
        /// Initializes the controller.
        /// </summary>
        /// <param name="requestContext">The request context.</param>
        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize(requestContext);

            if (this.SurveysRepository == null)
            {
                this.SurveysRepository = new FilesystemSurveysRepository(this.Server.MapPath("~/App_Data/"));
            }
        }

        /// <summary>
        /// Saves the specified user principal.
        /// </summary>
        /// <param name="userPrincipal">The user principal.</param>
        public ActionResult Save(IPrincipal userPrincipal)
        {
            foreach (var postedSurveyId in this.Request.Files.AllKeys)
            {
                var postedSurveyFile = this.Request.Files[postedSurveyId];
                var postedSurvey = default(Survey);

                using (var xmlTextReader = new XmlTextReader(this.Request.Files[postedSurveyId].InputStream))
                {
                    postedSurvey = SurveyXmlHelper.ReadFromElement(XDocument.Load(xmlTextReader).Root);

                    this.SurveysRepository.Save(postedSurvey);
                }
            }

            return new EmptyResult();
        }
    }
}
